home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Random2.0 / Source / TestArchive.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  125 lines

  1. //
  2. // TestArchive
  3. //
  4. // This program tests the archiving abilities of the Random System.
  5. //
  6.  
  7.  
  8. #import "Random.h"
  9. #import "StandardEngine.h"
  10. #import "ElkinsEngine.h"
  11. #import "R250Engine.h"
  12. #import <objc/typedstream.h>
  13. #import <stdio.h>
  14.  
  15.  
  16. //
  17. // test_archive()
  18. //
  19.  
  20. void test_archive(id myRand)
  21. {
  22.     double        buffer[1000];
  23.     double        foo;
  24.     int            i;
  25.     NXTypedStream    *myStream;
  26.     id            myNewRand;
  27.     
  28.     //
  29.     // Skip 1000 numbers
  30.     //
  31.     
  32.     printf("  Generating 1000 percentages...\n");
  33.     
  34.     for(i = 0; i < 1000; i++) {
  35.         foo = [myRand percent];
  36.     }
  37.     
  38.     //
  39.     // Archive the random:
  40.     //
  41.     
  42.     printf("  Archiving the random object to the file 'ArchivedRandom.rand'...\n");
  43.     
  44.     myStream = NXOpenTypedStreamForFile("ArchivedRandom.rand", NX_WRITEONLY);
  45.     [myRand write:myStream];
  46.     NXCloseTypedStream(myStream);
  47.     
  48.     //
  49.     // Generate and save a bunch of numbers:
  50.     //
  51.     
  52.     printf("  Generating and saving 1000 percentages...\n");
  53.     
  54.     for(i = 0; i < 1000; i++) {
  55.         buffer[i] = [myRand percent];
  56.     }
  57.     
  58.     //
  59.     // De-archive the random:
  60.     //
  61.     
  62.     printf("  Reading a copy of the archive from the file 'ArchivedRandom.rand'...\n");
  63.     
  64.     myStream = NXOpenTypedStreamForFile("ArchivedRandom.rand", NX_READONLY);
  65.     myNewRand = [[Random alloc] read:myStream];
  66.     NXCloseTypedStream(myStream);
  67.     
  68.     //
  69.     // Create 1000 more numbers, and compare them:
  70.     //
  71.     
  72.     printf("  Creating and comparing 1000 percentages to previous results...\n");
  73.     
  74.     for(i = 0; i < 1000; i++) {
  75.         if(buffer[i] != [myNewRand percent]) {
  76.         printf(">> Sequence diverged at %d!\n", i);
  77.         break;
  78.     }
  79.     }
  80.     
  81.     //
  82.     // Free data objects:
  83.     //
  84.     
  85.     printf("  Freeing temporary random number generator...\n");
  86.     
  87.     [myNewRand free];
  88.     
  89.     return;
  90. }
  91.  
  92.  
  93. //
  94. // main()
  95. //
  96.  
  97. int main(int argc, char *argv[])
  98. {
  99.     id myRand;
  100.     
  101.     printf("TestPercent: Testing StandardEngine class:\n");
  102.     myRand = [[Random alloc] initEngineClass:[StandardEngine class]];
  103.     test_archive(myRand);
  104.     printf("\n\n");
  105.     [myRand free];
  106.     
  107.     printf("TestPercent: Testing ElkinsEngine class:\n");
  108.     myRand = [[Random alloc] initEngineClass:[ElkinsEngine class]];
  109.     test_archive(myRand);
  110.     printf("\n\n");
  111.     [myRand free];
  112.     
  113.     printf("TestPercent: Testing R250Engine class:\n");
  114.     myRand = [[Random alloc] initEngineClass:[R250Engine class]];
  115.     test_archive(myRand);
  116.     printf("\n\n");
  117.     [myRand free];
  118.     
  119.     return 0;
  120. }
  121.  
  122.  
  123. //
  124. // End of file.
  125. //